<%--
    Document   : update
    Created on : Jul 1, 2012, 12:30:17 AM
    Author     : aspr0
--%>



<%@page import="is.model.dao.RegionDao"%>
<%@page import="is.interfaces.Dao"%>
<%@page import="is.NullUtil"%>
<%@page import="is.model.exceptions.EmptyValueException"%>
<%@page import="is.model.exceptions.ObjectNotFoundException"%>
<%@page import="is.web.Message"%>
<%@page import="java.lang.NumberFormatException"%>
<%@page import="is.web.GetParam"%>
<%@page import="is.web.Form"%>
<%@page import="is.web.SelectOptions"%>
<%@page import="java.util.List"%>
<%@page import="is.model.bean.Region"%>
<%@page import="is.model.bean.Employee"%>
<%@page import="java.sql.SQLException"%>
<%@page import="is.model.bean.Region"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="is.model.DataSource"%>
<%@page import="is.model.bean.Region"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    request.setCharacterEncoding(is.Conf.ENCODING); // !!!

    String datatype = "";
    String content = "";

    try
    {
        DataSource.establishConnection();

        Region initialRegionBean = new Region();
        Dao<Region> regionDao = new RegionDao();

        regionDao.setInitialBean(initialRegionBean);

        List<Region> regions = new LinkedList<Region>();

        /* Retrieving lists for building option selects */
        Integer regionsCount = regionDao.count();
        regions = regionDao.retrieveMultiple(0, regionsCount, -1);

        LinkedList<String> selects = new LinkedList<String>();

        selects.add(new SelectOptions<Region>().generate(regions, 0, true, "parentId", 1));

        Region emptyRegion = new Region();

        String form = Form.generate(emptyRegion, selects, 0);

        datatype = emptyRegion.getTableName();
        content = form;

        if (request.getMethod().equals("POST"))
        {
%>

<jsp:useBean id="regionNew" class="is.model.bean.Region" scope="request"/>
<jsp:setProperty name="regionNew" property="*"/>

<%
            NullUtil.prove(regionNew.getName());

            regionDao.setInitialBean(regionNew);
            regionDao.create();
            response.sendRedirect("?"+GetParam.INFO+"=2");
        }

        DataSource.closeConnection();
    }
    catch(EmptyValueException e)
    {
        content = Message.genMsgWarning("1") + content;
    }
    catch (SQLException e)
    {
        e.printStackTrace();
        response.sendRedirect("?"+GetParam.ERROR+"=0");
        return;
    }
    catch (Exception e)
    {
        if(e.getCause().getCause().getClass() == new NumberFormatException().getClass())
        {
            content = Message.genMsgWarning("0") + content;
        }
        else
        {
            e.printStackTrace();
            response.sendRedirect("?"+GetParam.ERROR+"=1");
            return;
        }
    }
%>

<jsp:include page="../template/create.jsp">
    <jsp:param name="datatype" value="<%=datatype%>"/>
    <jsp:param name="content" value="<%=content%>"/>
</jsp:include>